package com.itheima.service.impl;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandServiceImpl implements BrandService {

    private SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public List<Brand> selectAll() {

        SqlSession sqlSession=sqlSessionFactory.openSession();

        List<Brand> brands = sqlSession.getMapper(BrandMapper.class).selectAll();

        sqlSession.close();

        return brands;
    }

    @Override
    public void add(Brand brand) {
        SqlSession sqlSession=sqlSessionFactory.openSession();


        sqlSession.getMapper(BrandMapper.class).add(brand);

        sqlSession.commit();

        sqlSession.close();

    }
    @Override
    public void del(int id) {
        SqlSession sqlSession=sqlSessionFactory.openSession();


        sqlSession.getMapper(BrandMapper.class).del(id);

        sqlSession.commit();

        sqlSession.close();

    }

    @Override
    public void delAll(int ids[]) {
        SqlSession sqlSession=sqlSessionFactory.openSession();


        sqlSession.getMapper(BrandMapper.class).delAll(ids);

        sqlSession.commit();

        sqlSession.close();

    }

    @Override
    public PageBean<Brand> selectAllPage(int begin, int size) {


        SqlSession sqlSession=sqlSessionFactory.openSession();

        int beginPage=(begin-1)*size;

        int sizePage=size;

        List<Brand> brands = sqlSession.getMapper(BrandMapper.class).selectByPage(beginPage,sizePage);


        int count = sqlSession.getMapper(BrandMapper.class).selectTotalCount();

        PageBean<Brand> pageBean=new PageBean<>();

        pageBean.setRows(brands);
        pageBean.setTotalCount(count);

        sqlSession.close();

        return pageBean;
    }

    @Override
    public PageBean<Brand> selectByPageAndCondition(int begin, int size, Brand brand) {
        SqlSession sqlSession=sqlSessionFactory.openSession();

        int beginPage=(begin-1)*size;

        int sizePage=size;

        if(brand.getBrandName()!=null && brand.getBrandName().length()>0){
            brand.setBrandName("%"+brand.getBrandName()+"%");
        }

        if(brand.getCompanyName()!=null && brand.getCompanyName().length()>0){
            brand.setCompanyName("%"+brand.getCompanyName()+"%");
        }

        System.out.println(brand.getBrandName());
        System.out.println(brand.getCompanyName());

        List<Brand> brands = sqlSession.getMapper(BrandMapper.class).selectByPageAndCondition(beginPage,sizePage,brand);


        int count = sqlSession.getMapper(BrandMapper.class).selectTotalCountByCondition(brand);

        PageBean<Brand> pageBean=new PageBean<>();

        pageBean.setRows(brands);
        pageBean.setTotalCount(count);

        sqlSession.close();

        return pageBean;
    }

}
